package com.eke.serviceimpl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.eke.bean.Article;
import com.eke.bean.ArticleType;
import com.eke.bean.QuestionFrom;
import com.eke.dao.ArticleDao;
import com.eke.dao.ArticleTypeDao;
import com.eke.dao.QuestionFromDao;
import com.eke.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Resource
	private ArticleDao artd;
	@Resource
	private ArticleTypeDao articleTypeDao;
	@Resource
	private QuestionFromDao questionFromDao;

	/**
	 * 添加文章
	 * 
	 * @param article
	 *            文章对象
	 * @return {@link Boolean} 添加成功返回true 失败返回false
	 */
	public boolean addArticle(Article article) {
		// TODO Auto-generated method stub
		if (article.getArticleTitle() == null) {
			return false;
		} else {
			return (artd.insert(article) > 0);
		}
	}

	/**
	 * 通过文章类型父ID查询出文章类型
	 * 
	 * @param articleSubTypeId
	 *            文章类型父id
	 * @return {@link List}<{@link ArticleType}> 返回文章类型集合
	 */
	public List<ArticleType> queryArticleTypeBySubID(int articleSubTypeId) {
		// TODO Auto-generated method stub
		return articleTypeDao.qurryArticleType(articleSubTypeId);
	}

	/**
	 * 根据文章id查询文章详情
	 * 
	 * @param articleId
	 *            文章id
	 * @return {@link Article} 返回文章对象
	 */
	public Article queryArticleByArticleId(int articleId) {
		// TODO Auto-generated method stub
		return artd.queryArticleDeatilsByArticleId(articleId);
	}

	/**
	 * 查询文章总数
	 * 
	 * @return int
	 */
	public int queryArticleTotal() {
		// TODO Auto-generated method stub
		return artd.queryArticleTotal();
	}

	public List<Article> queryArticle(int startRow, int pageSize) {
		return artd.qurryAllArticle(startRow, pageSize);
	}

	/*------------------------------------------------------------*/
	/**
	 * 查询问题总数
	 * 
	 */
	public int queryQuestionTotal() {
		// TODO Auto-generated method stub
		return artd.queryQuestionTotal();
	}

	/**
	 * 结合分页查询所有问题
	 * 
	 * @param startRow
	 *            开始页
	 * @param pageSize
	 *            数量
	 * 
	 * @return {@link List}<{@link Article}> 返回文章集合
	 */
	public List<Article> queryAllQuestion(int startRow, int pageSize) {
		// TODO Auto-generated method stub
		return artd.queryAllQuestion(startRow, pageSize);
	}

	/**
	 * 根据文章类型父id查询
	 * 
	 * @param articleTypeId
	 *            文章类型id
	 * @return {@link ArticleType} 返回文章类型对象
	 */
	
	public ArticleType qurryArticleTypeByParentId(int articleTypeId) {
		// TODO Auto-generated method stub
		return null;
	}

	
	/**
	 * 分页查询出拼接问题
	 */
	public List<QuestionFrom> queryQuestionByFrom(int startRow, int pageSize) {
		// TODO Auto-generated method stub
		return questionFromDao.qurryPagingQuestion(startRow, pageSize);
	}

	/**
	 * 分页查询出拼接文章
	 */
	
	public List<QuestionFrom> qurryPagingArticle(int startRow, int pageSize) {
		// TODO Auto-generated method stub
		return questionFromDao.qurryPagingArticle(startRow, pageSize);
	}

	/**
	 * 随机查询10篇文章
	 * 
	 * @return List<{@link Article}>
	 */
	
	public List<Article> queryTenArticle() {
		// TODO Auto-generated method stub
		return artd.queryTenArticle();
	}

	/**
	 * 增加评论人数
	 * 
	 * @param articleId
	 *            文章或问题ID
	 * @return boolean
	 */
	public boolean addCommentNumber(int articleId) {
		// TODO Auto-generated method stub
		if (artd.addCommentNumber(articleId) > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 根据用户id查询该用户所有文章
	 */
	
	public List<Article> queryAllArticleById(Integer userId, Integer startRow, Integer pageSize) {
		return artd.queryAllArticleById(userId, startRow, pageSize);
	}

	/**
	 * 根据文章id删除文章
	 */
	
	public boolean delectArticleById(Integer articleId) {
		return artd.delectArticleById(articleId);
	}

	/**
	 * 根据文章名查文章
	 */
	
	public List<Article> queryArticleByName(String articleTitle) {
		// TODO Auto-generated method stub
		return artd.queryArticleByName(articleTitle);
	}

	/**
	 * 查询所有文章
	 */
	
	public List<Article> queryArticle() {
		// TODO Auto-generated method stub
		return artd.queryArticle();
	}

	/**
	 * 查询所有问题
	 */
	
	public List<Article> queryQuestion() {
		// TODO Auto-generated method stub
		return artd.queryQuestion();
	}

	/**
	 * 结合文章标题分页查询文章
	 */
	
	public List<Article> queryArticleByTitleLimit(String articleTitle, int startRow, int pageSize) {
		// TODO Auto-generated method stub
		return artd.queryArticleByTitleAndLimit(articleTitle, startRow, pageSize);
	}

	
	public List<Article> queryAllQuestionById(Integer userId) {
		// TODO Auto-generated method stub
		return artd.queryAllQuestionById(userId);
	}

	
	public boolean delectQuestionById(Integer articleId) {
		// TODO Auto-generated method stub
		return artd.delectQuestionById(articleId);
	}

	/**
	 * 更改问题状态为已解决
	 * 
	 * @return {@link Boolean}
	 */
	
	public boolean updateQuestionState(Integer questionId) {
		// TODO Auto-generated method stub
		return artd.updateQuestionState(questionId);
	}

	/**
	 * 判断是否可以采纳评论
	 * 
	 * @param questionId
	 * @return question_state = 0 已解决 false question_state = 1 未解决 true
	 */
	
	public boolean isCanAgree(Integer questionId) {
		// TODO Auto-generated method stub
		return artd.isCanAgree(questionId).getQuestionState() == 1;
	}

	/**
	 * 结合问题标题分页查询问题
	 * 
	 * @param articleTitle
	 *            问题标题
	 * @param startRow
	 *            开始页
	 * @param pageSize
	 *            数量
	 * @return {@link List}<{@link Article}> 返回文章集合
	 */
	
	public List<Article> queryQusetionByTitleLimit(String articleTitle, int startRow, int pageSize) {
		return artd.queryQusetionByTitleAndLimit(articleTitle, startRow, pageSize);
	}

	/**
	 * 修改文章状态
	 * 
	 * @param id
	 *            文章id
	 * @return {@link Boolean} 修改成功true 失败false
	 */
	
	public boolean updateArticleState(Integer id) {
		// TODO Auto-generated method stub
		return artd.updateArticleState(id);
	}

}
